var gulp = require('gulp')
var	less = require('gulp-less');
var connect =require('gulp-connect');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var minifycss = require('gulp-minify-css');
var rename = require('gulp-rename');
var ngAnnotate = require('gulp-ng-annotate');
var imagemin = require('gulp-imagemin');
var htmlmin = require('gulp-htmlmin');
// 合并bower的js并压缩  
gulp.task('bowerjs',function(){
	gulp.src(['bower_components/jquery/jquery.min.js','src/js/angualrjs1.2.3.js','bower_components/bootstrap/docs/assets/js/bootstrap.min.js','bower_components/echarts/dist/echarts.min.js','bower_components/angular-ui-router/release/angular-ui-router.min.js','bower_components/ng-dialog/js/ngDialog.min.js','bower_components/angular-local-storage/dist/angular-local-storage.min.js','bower_components/bootstrap-datetimepicker/src/js/bootstrap-datetimepicker.js'])
	.pipe(concat("bowerjs.js"))
	.pipe(gulp.dest('dist/bowerjs'))
	.pipe(connect.reload());
})
// 合并bower的css并压缩
gulp.task('bowercss',function(){
	gulp.src(['bower_components/**/*.min.css'])
	.pipe(concat("bowercss.css"))
	.pipe(minifycss())
	.pipe(gulp.dest('dist/bowercss'))
	.pipe(connect.reload());
})
// 复制src的js文件并压缩 
gulp.task('minjs',function(){
	gulp.src(['src/js/**/*.js'])
	.pipe(ngAnnotate())
	.pipe(uglify())
	.pipe(gulp.dest('dist/js'))
	.pipe(connect.reload());
})
/*复制src的css文件并压缩*/
gulp.task("mincss",function(){
	gulp.src(['src/css/**/*.css'])
	.pipe(minifycss())
	.pipe(gulp.dest('dist/css'))
	.pipe(connect.reload());
});
/*复制src的html文件并压缩*/
gulp.task('minhtml', function () {
    var options = {
        removeComments: true,//清除HTML注释
        collapseWhitespace: true,//压缩HTML
        collapseBooleanAttributes: true,//省略布尔属性的值 <input checked="true"/> ==> <input />
        removeEmptyAttributes: true,//删除所有空格作属性值 <input id="" /> ==> <input />
        removeScriptTypeAttributes: true,//删除<script>的type="text/javascript"
        removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
        minifyJS: true,//压缩页面JS
        minifyCSS: true//压缩页面CSS
    };
    gulp.src('src/view/**/*.html')
        .pipe(htmlmin(options))
		.pipe(gulp.dest('dist/view'))
		.pipe(connect.reload());
});

/*合并图片并压缩*/
gulp.task("imagesmin",function(){
	gulp.src(['src/imgs/*'])
	.pipe(gulp.dest('dist/imgs'));
});
/*合并my97日历插件并压缩*/
gulp.task("my97imagesmin",function(){
	gulp.src(['src/js/**/**/*.js','src/js/**/**/*.css','src/js/**/**/*'])
	.pipe(gulp.dest('dist/js'));
});

/*启动服务*/
gulp.task("server",function(){
	connect.server({
		root:'dist',
		port: 8866,
		livereload: true
	})
});
/*监测更新*/
gulp.task('watch',function(){
	// gulp.watch(["src/**/*.css","src/**/*.js","src/**/*.html"],['mincss','minjs','minhtml']);
	gulp.watch(["src/**/*.css"],['mincss']);
	gulp.watch(["src/**/*.js"],['minjs']);
	gulp.watch(["src/**/*.html","src/**/**/*.html"],['minhtml']);
})
/*默认执行'minjs',*/
gulp.task('default',['watch','server','mincss','minhtml','my97imagesmin']);